package code.c11;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
 * @author thinkpad - 83start
 * @version v1.0
 * @create 2022/11/24 10:39
 * @package code.c11
 * @description GUI 的键盘监听测试
 */

public class TestKeyEvent {
    public static void main(String[] args) {
        // 创建 frame 并设置
        JFrame frame = new JFrame();
        frame.setBounds(300, 500, 500, 300);
        frame.setLayout(new FlowLayout());

        // 创建组件
        JTextField numField = new JTextField(10);
        JTextField num2Field = new JTextField(10);
        frame.add(numField);
        frame.add(num2Field);

        // 设置键盘监听
        numField.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (KeyEvent.VK_ENTER == e.getKeyCode()) {
                    int num = Integer.parseInt(numField.getText());
                    System.out.println(num);
                    num2Field.setText(String.valueOf((int) Math.pow(num, 2)));
                }
            }
        });
        frame.setVisible(true);
    }
}
